package com.ruoyi.quanzi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quanzi.domain.SunPostCollection;
import com.ruoyi.quanzi.service.ISunPostCollectionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 帖子收藏Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/quanzi/collection")
public class SunPostCollectionController extends BaseController
{
    @Autowired
    private ISunPostCollectionService sunPostCollectionService;

    /**
     * 查询帖子收藏列表
     */
    @PreAuthorize("@ss.hasPermi('quanzi:collection:list')")
    @GetMapping("/list")
    public TableDataInfo list(SunPostCollection sunPostCollection)
    {
        startPage();
        List<SunPostCollection> list = sunPostCollectionService.selectSunPostCollectionList(sunPostCollection);
        return getDataTable(list);
    }

    /**
     * 导出帖子收藏列表
     */
    @PreAuthorize("@ss.hasPermi('quanzi:collection:export')")
    @Log(title = "帖子收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SunPostCollection sunPostCollection)
    {
        List<SunPostCollection> list = sunPostCollectionService.selectSunPostCollectionList(sunPostCollection);
        ExcelUtil<SunPostCollection> util = new ExcelUtil<SunPostCollection>(SunPostCollection.class);
        util.exportExcel(response, list, "帖子收藏数据");
    }

    /**
     * 获取帖子收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('quanzi:collection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sunPostCollectionService.selectSunPostCollectionById(id));
    }

    /**
     * 新增帖子收藏
     */
    @PreAuthorize("@ss.hasPermi('quanzi:collection:add')")
    @Log(title = "帖子收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SunPostCollection sunPostCollection)
    {
        return toAjax(sunPostCollectionService.insertSunPostCollection(sunPostCollection));
    }

    /**
     * 修改帖子收藏
     */
    @PreAuthorize("@ss.hasPermi('quanzi:collection:edit')")
    @Log(title = "帖子收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SunPostCollection sunPostCollection)
    {
        return toAjax(sunPostCollectionService.updateSunPostCollection(sunPostCollection));
    }

    /**
     * 删除帖子收藏
     */
    @PreAuthorize("@ss.hasPermi('quanzi:collection:remove')")
    @Log(title = "帖子收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sunPostCollectionService.deleteSunPostCollectionByIds(ids));
    }

    /**
     * 用户收藏帖子
     */
    @PostMapping("/collect/{postId}")
    public AjaxResult collectPost(@PathVariable Long postId)
    {
        Long userId = getUserId();
        return toAjax(sunPostCollectionService.collectPost(userId, postId));
    }

    /**
     * 用户取消收藏帖子
     */
    @DeleteMapping("/uncollect/{postId}")
    public AjaxResult uncollectPost(@PathVariable Long postId)
    {
        Long userId = getUserId();
        return toAjax(sunPostCollectionService.uncollectPost(userId, postId));
    }

    /**
     * 获取用户收藏的帖子列表
     */
    @GetMapping("/user/{userId}")
    public TableDataInfo getUserCollections(@PathVariable Long userId)
    {
        startPage();
        List<SunPostCollection> list = sunPostCollectionService.selectCollectionsByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 检查用户是否收藏了某个帖子
     */
    @GetMapping("/check/{postId}")
    public AjaxResult checkCollection(@PathVariable Long postId)
    {
        Long userId = getUserId();
        boolean isCollected = sunPostCollectionService.isPostCollectedByUser(userId, postId);
        return success(isCollected);
    }
}